﻿using Edge.Repository.Model.EMS;
using HYRN.Common.ConvertUtilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EMSClient.CForm
{
    public partial class FormEquipEdit : Form
    {
        EmsEquipService dal = new EmsEquipService();
        public int _pkid = 0;
        EmsEquipModel _model = null;
        public FormEquipEdit(int id = 0)
        {
            InitializeComponent();
            _pkid = id;
            if (id > 0)
            {
                EmsEquipModel model = dal.QueryById(id);
                if (model != null)
                {
                    _model = model;
                    this.textBox1.Text = model.Name;
                    this.textBox2.Text = model.Code;
                    this.textBox3.Text = model.Remark;
                    this.textBox4.Text = model.Ip;

                    this.numericUpDown1.Value = model.WenShiDuCount;

                    if (model.Type == 1) { radioButton1.Checked = true; }
                    else { radioButton2.Checked = true; }

                }
                else { _model = null; }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            #region ip 验证
            int ipCount = dal.Query(F => F.Id != _pkid && F.Ip == this.textBox4.Text.Trim()).Count();
            if (ipCount > 0)
            {
                MessageBox.Show("IP 已经录入，请确认后填写！");
                return;
            }
            #endregion



            if (_pkid == 0)
            {

                

                #region add

                EmsEquipModel model = new EmsEquipModel();
                model.Name = this.textBox1.Text;
                model.Code = this.textBox2.Text;
                model.Remark = this.textBox3.Text;

                model.Ip = this.textBox4.Text.Trim();


                if (radioButton1.Checked) { model.Type = 1; } else { model.Type = 2; }
                model.WenShiDuCount = this.numericUpDown1.Value.ObjToInt();

                int i = dal.Add(model);
                if (i > 0) { MessageBox.Show("设备操作成功！"); this.DialogResult = DialogResult.OK; }
                else { MessageBox.Show("系统异常，请联系管理员！"); }
                #endregion
            }
            else
            {


                #region update

                if (_model == null)
                {
                    _model = dal.QueryById(_pkid);
                }
                if (_model == null)
                {
                    MessageBox.Show("数据异常，请确认后填写！");
                    return;
                }
           


                _model.Name = this.textBox1.Text;
                _model.Code = this.textBox2.Text;
                _model.Remark = this.textBox3.Text;
                _model.Ip = this.textBox4.Text;

                if (radioButton1.Checked) { _model.Type = 1; } else { _model.Type = 2; }

                _model.WenShiDuCount = this.numericUpDown1.Value.ObjToInt();

                bool i = dal.Update(_model);
                if (i) { MessageBox.Show("设备操作成功！"); this.DialogResult = DialogResult.OK; }
                else { MessageBox.Show("系统异常，请联系管理员！"); }
                #endregion
            }
        }
    }
}
